package com.immo.libcommon.view;

import com.immo.libcommon.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

/**
 * @author Administrator
 * @content 是否有消息已读未读
 * @date 2017/11/25
 */
public class MessageTagView extends AppCompatImageView {
    private boolean hasMessage;
    private int messageNum;


    public boolean isHasMessage() {
        return hasMessage;
    }

    /**
     * @param hasMessage 是否有新消息
     * @param messageNum 消息数量
     */
    public void setHasMessage(boolean hasMessage, int messageNum) {
        this.hasMessage = hasMessage;
        this.messageNum = messageNum;
    }

    public MessageTagView(Context context) {
        this(context, null);
    }

    public MessageTagView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MessageTagView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MessageTagView);
        hasMessage = ta.getBoolean(R.styleable.MessageTagView_hasMessage, false);
        ta.recycle();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (hasMessage) {
            drawTRCircle(canvas);
        } else {
            drawNumberCircle(canvas, messageNum);
        }
    }

    /**
     * 在右上角绘制红色的圆心
     */
    private void drawTRCircle(Canvas canvas) {
        int radius = (int) (5 * getResources().getDisplayMetrics().density);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        int drawableHeight = getDrawable().getIntrinsicHeight();
        int drawableWidth = getDrawable().getIntrinsicWidth();
        int dia = 2 * radius;
        int padding = 1;
        int cx;
        int cy;
        if (width >= drawableWidth + dia + padding) {
            cx = width / 2 + drawableWidth / 2 + padding;
        } else {
            cx = width - radius - padding;
        }
        if (height >= drawableHeight + dia + padding) {
            cy = height / 2 - drawableWidth / 2;
        } else {
            cy = radius + padding;
        }
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawCircle(cx + 10, cy, radius, paint);

    }


    /**
     * 画带数字的角标
     *
     * @param canvas             画板
     * @param notificationNumber 数目
     */
    private void drawNumberCircle(Canvas canvas, int notificationNumber) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        int redCircleSize = 15;
        RectF rectF;
        if (notificationNumber <= 0) return;
        paint.setColor(Color.RED);
        if (notificationNumber < 10) {
            rectF = new RectF(getWidth() - redCircleSize * 2 - 8, 0, getWidth() - 8, redCircleSize * 2);
        } else if (notificationNumber < 100) {
            rectF = new RectF(getWidth() - redCircleSize * 3 - 8, 0, getWidth() - 8, redCircleSize * 2);
        } else {
            rectF = new RectF(getWidth() - redCircleSize * 4 - 8, 0, getWidth() - 8, redCircleSize * 2);

        }
//        Paint paint1=new Paint();
//        paint1.setColor(Color.WHITE);
//        RectF rect1=new RectF(rectF.left+3,rectF.top+3,rectF.right-3,rectF.bottom-3);
        canvas.drawRoundRect(rectF, redCircleSize, redCircleSize, paint);
//        canvas.drawRoundRect(rect1,redCircleSize,redCircleSize,paint1);
        paint.setColor(Color.WHITE);
        paint.setTextSize(redCircleSize * 3 / 2);
        paint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
        int baseline = (int) ((rectF.bottom + rectF.top - fontMetrics.bottom - fontMetrics.top) / 2);
        if (notificationNumber < 100) {
            canvas.drawText(String.valueOf(notificationNumber), getWidth() - rectF.width() / 2 - 7, baseline - 2, paint);
        } else {
            canvas.drawText("99+", getWidth() - rectF.width() / 2 - 7, baseline - 2, paint);
        }
    }
}
